<template>
  <el-dialog
    :title="!dataForm.tableId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="表名称" prop="tableName">
      <el-input v-model="dataForm.tableName" placeholder="表名称"></el-input>
    </el-form-item>
    <el-form-item label="表描述" prop="tableComment">
      <el-input v-model="dataForm.tableComment" placeholder="表描述"></el-input>
    </el-form-item>
    <el-form-item label="关联子表的表名" prop="subTableName">
      <el-input v-model="dataForm.subTableName" placeholder="关联子表的表名"></el-input>
    </el-form-item>
    <el-form-item label="子表关联的外键名" prop="subTableFkName">
      <el-input v-model="dataForm.subTableFkName" placeholder="子表关联的外键名"></el-input>
    </el-form-item>
    <el-form-item label="实体类名称" prop="className">
      <el-input v-model="dataForm.className" placeholder="实体类名称"></el-input>
    </el-form-item>
    <el-form-item label="使用的模板（crud单表操作 tree树表操作）" prop="tplCategory">
      <el-input v-model="dataForm.tplCategory" placeholder="使用的模板（crud单表操作 tree树表操作）"></el-input>
    </el-form-item>
    <el-form-item label="生成包路径" prop="packageName">
      <el-input v-model="dataForm.packageName" placeholder="生成包路径"></el-input>
    </el-form-item>
    <el-form-item label="生成模块名" prop="moduleName">
      <el-input v-model="dataForm.moduleName" placeholder="生成模块名"></el-input>
    </el-form-item>
    <el-form-item label="生成业务名" prop="businessName">
      <el-input v-model="dataForm.businessName" placeholder="生成业务名"></el-input>
    </el-form-item>
    <el-form-item label="生成功能名" prop="functionName">
      <el-input v-model="dataForm.functionName" placeholder="生成功能名"></el-input>
    </el-form-item>
    <el-form-item label="生成功能作者" prop="functionAuthor">
      <el-input v-model="dataForm.functionAuthor" placeholder="生成功能作者"></el-input>
    </el-form-item>
    <el-form-item label="生成代码方式（0zip压缩包 1自定义路径）" prop="genType">
      <el-input v-model="dataForm.genType" placeholder="生成代码方式（0zip压缩包 1自定义路径）"></el-input>
    </el-form-item>
    <el-form-item label="生成路径（不填默认项目路径）" prop="genPath">
      <el-input v-model="dataForm.genPath" placeholder="生成路径（不填默认项目路径）"></el-input>
    </el-form-item>
    <el-form-item label="其它生成选项" prop="options">
      <el-input v-model="dataForm.options" placeholder="其它生成选项"></el-input>
    </el-form-item>
    <el-form-item label="创建者" prop="createBy">
      <el-input v-model="dataForm.createBy" placeholder="创建者"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新者" prop="updateBy">
      <el-input v-model="dataForm.updateBy" placeholder="更新者"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          tableId: 0,
          tableName: '',
          tableComment: '',
          subTableName: '',
          subTableFkName: '',
          className: '',
          tplCategory: '',
          packageName: '',
          moduleName: '',
          businessName: '',
          functionName: '',
          functionAuthor: '',
          genType: '',
          genPath: '',
          options: '',
          createBy: '',
          createTime: '',
          updateBy: '',
          updateTime: '',
          remark: ''
        },
        dataRule: {
          tableName: [
            { required: true, message: '表名称不能为空', trigger: 'blur' }
          ],
          tableComment: [
            { required: true, message: '表描述不能为空', trigger: 'blur' }
          ],
          subTableName: [
            { required: true, message: '关联子表的表名不能为空', trigger: 'blur' }
          ],
          subTableFkName: [
            { required: true, message: '子表关联的外键名不能为空', trigger: 'blur' }
          ],
          className: [
            { required: true, message: '实体类名称不能为空', trigger: 'blur' }
          ],
          tplCategory: [
            { required: true, message: '使用的模板（crud单表操作 tree树表操作）不能为空', trigger: 'blur' }
          ],
          packageName: [
            { required: true, message: '生成包路径不能为空', trigger: 'blur' }
          ],
          moduleName: [
            { required: true, message: '生成模块名不能为空', trigger: 'blur' }
          ],
          businessName: [
            { required: true, message: '生成业务名不能为空', trigger: 'blur' }
          ],
          functionName: [
            { required: true, message: '生成功能名不能为空', trigger: 'blur' }
          ],
          functionAuthor: [
            { required: true, message: '生成功能作者不能为空', trigger: 'blur' }
          ],
          genType: [
            { required: true, message: '生成代码方式（0zip压缩包 1自定义路径）不能为空', trigger: 'blur' }
          ],
          genPath: [
            { required: true, message: '生成路径（不填默认项目路径）不能为空', trigger: 'blur' }
          ],
          options: [
            { required: true, message: '其它生成选项不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '创建者不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateBy: [
            { required: true, message: '更新者不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.tableId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.tableId) {
            this.$http({
              url: this.$http.adornUrl(`/ruoyiapisystem/gentable/info/${this.dataForm.tableId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.tableName = data.genTable.tableName
                this.dataForm.tableComment = data.genTable.tableComment
                this.dataForm.subTableName = data.genTable.subTableName
                this.dataForm.subTableFkName = data.genTable.subTableFkName
                this.dataForm.className = data.genTable.className
                this.dataForm.tplCategory = data.genTable.tplCategory
                this.dataForm.packageName = data.genTable.packageName
                this.dataForm.moduleName = data.genTable.moduleName
                this.dataForm.businessName = data.genTable.businessName
                this.dataForm.functionName = data.genTable.functionName
                this.dataForm.functionAuthor = data.genTable.functionAuthor
                this.dataForm.genType = data.genTable.genType
                this.dataForm.genPath = data.genTable.genPath
                this.dataForm.options = data.genTable.options
                this.dataForm.createBy = data.genTable.createBy
                this.dataForm.createTime = data.genTable.createTime
                this.dataForm.updateBy = data.genTable.updateBy
                this.dataForm.updateTime = data.genTable.updateTime
                this.dataForm.remark = data.genTable.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/ruoyiapisystem/gentable/${!this.dataForm.tableId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'tableId': this.dataForm.tableId || undefined,
                'tableName': this.dataForm.tableName,
                'tableComment': this.dataForm.tableComment,
                'subTableName': this.dataForm.subTableName,
                'subTableFkName': this.dataForm.subTableFkName,
                'className': this.dataForm.className,
                'tplCategory': this.dataForm.tplCategory,
                'packageName': this.dataForm.packageName,
                'moduleName': this.dataForm.moduleName,
                'businessName': this.dataForm.businessName,
                'functionName': this.dataForm.functionName,
                'functionAuthor': this.dataForm.functionAuthor,
                'genType': this.dataForm.genType,
                'genPath': this.dataForm.genPath,
                'options': this.dataForm.options,
                'createBy': this.dataForm.createBy,
                'createTime': this.dataForm.createTime,
                'updateBy': this.dataForm.updateBy,
                'updateTime': this.dataForm.updateTime,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
